![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Many programming groups redefine the prefixes shown in Tables 3.1, 3.2, and 3.3 to suit their own needs and preferences. For example, the Boolean prefix bln is sometimes changed to bool and the variant prefix vnt is often changed to var. It does not matter what prefixes you use, as long as everyone on your project uses the same values.
Some developers use prefixes for subroutines and functions as well as variables and controls. For example, a function declared private within a module that returns the factorial of a number as a long integer might be named mlngFactorial. PurposeHungarian notation provides additional information about a variables scope and data type. When you see a variable named gdblFieldLength, you know it is a global variable declared as a double precision number. Hungarian notation also prevents some kinds of name space conflicts. For instance, in the following code, the variable count is defined at the module level and within the subroutine. The definition of count inside the subroutine hides the module-level variable. Inside the subroutine, count refers to the local variable, not the module-level variable. Name collisions like this one can be confusing.
Private count As Integer
Private Sub DoSomething()
Dim count As integer
For count = 1 To 100
:
Next count
End Sub
The best way to avoid this sort of name confusion is to not use the same name for multiple purposes. There would be no problem if the module-level variable was named control_count instead of count. Hungarian notation also protects against this kind of name conflict. In this example, the module-level variable would be renamed mCount and there would be no conflict. A particularly useful example of this protection is in naming controls. Controls and their data are logically related. A TextBox control and the text it contains are very similar in purpose. In fact, the TextBox controls default property is Text. That means a program can use Text1 and Text1.Text interchangeably. If you use a control type prefix to name controls and data type prefixes to name variables that contain a controls value, you can give the control and its value similar names without confusion. For example, a TextBox containing a customers name might be named txtCustomer. The program could store and manipulate the TextBoxs value in the string variable strCustomer. The similarity between these two names makes their relationship clear while their prefixes make it obvious that they are not quite the same. One drawback to Hungarian notation is that a variable name is tied to a specific implementation as well as the variables purpose. For instance, the name mintNumEmployees is a module-level integer variable. During development, if you decide to change the variable to a long integer, you must rename the variable. This is somewhat annoying because the meaning of the variable has not changed. Ideally, you should not need to rename a variable unless its meaning has changed. This is one of the prices you pay for using Hungarian notation. A Word of CautionYou should use Hungarian notation with some discretion. A bad variable name with a fancy prefix is still a bad variable name. For instance, suppose you are reading the code in a clean room control program and you come across the variable gdblDHum. You can tell this is a global double precision variable, but that is all. You do not know what the variable means, how it is used, or where it is declared. Unless there is a useful comment nearby, you will have to search for Public gdblDHum and hope a comment near the variables declaration can shed some light on the situation. On the other hand, suppose the variables name is DesiredHumidity. Now you have some idea of the variables purpose and use. If you know a bit about the application, you may know or guess that the value is a floating-point number. If the variable is named gdblDesiredHumidity, you know the scope, data type, and purpose of the variable. Some developers think Hungarian notation is a magic solution that will make any program maintainable. Prefixes only help if the variables base names are informative, too. Without a meaningful base name, knowing the variables data type is worthless. If you have to pick between Hungarian notation and meaningful names, meaningful names are the right choice every time.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|